#!/usr/bin/env python
# encoding: utf-8
'''
@author: liuchong
@contact: lucas@007vin.com
@software: garner
@file: banzhou.py
@time: 2020/8/24 2:44 下午
@desc:
'''

import sys
sys.path.append('../../')
import pandas as pd
import re
from pymongo import MongoClient
import ujson
from models.porsches.porsches import CarsPartNumNew, CarsSaDesc, CarsStructureCollectionNew
from models.porsches.porsches_vb import Vb
from scripts.fenci import remove_tt
from cars_scripts.service.AudiService import cleanTransmission, cleanDrive, cleanCylinder, cleanPower
from cars_scripts.service.add_date import audi_do_filter_extra_info, mergeDate

s = ['3缸', '三缸', '4缸', '四缸', '5缸', '五缸', '6缸', '六缸', '8缸', '八缸', '10缸', '十缸', '12缸', '十二缸']
qd = ['全轮驱动', '前驱动', '前部驱动', '四轮驱动']
fuel = ['汽油', '柴油']
bsx = ['7档', '7档自动', '7档手动', '7档双离合', '七档', '七档自动', '七档手动', '七档双离合', '6档', '6档自动', '6档手动', '6挡手自一体',
       '六档', '六档自动', '六档手动', '5档', '5档自动', '5档手动', '五档', '五档自动', '五档手动', '5挡手自一体',
       '4档', '4档自动', '4档手动', '四档', '四档自动', '四档手动', '8档', '8档自动', '8档手动', '8挡手自一体',
       '八档', '八档自动', '八档手动', '3档', '3档自动', '3档手动', '三档', '三档自动', '三档手动', '无级自动']
pl = ['4.0', '2.0', '3.0', '3.3', '4.5', '4.7', '2.5', '5.0', '3.2', '2.8', '3.6', '2.7', '5.4', '3.7', '3.4', '5.7', '3.8', '4.8', '2.9', '4.2', '4.6', '1.8', '1.7', '1.1', '1.3', '1.5', '1.6', '2.2', '2.4']
fdjdm = ['DKD', 'DKDA', '0478', '0479', '047E', '047F', '3101', '3102', '3103', '3104', '3150', '93003', '93004', '93005', '93006', '93007', '93008', '93009', '93010', '93013', '93015', '93016', '93017', '93019', '93060', '93061', '93062', '93063', '93064', '93065', '93066', '2801', '2802', '2803', '2804', '2809', '2810', '2811', '2812', '2813', '2814', '2815', '2816', '2817', '2818', '0474', '0475', '0476', '4401', '4402', '4403', '4404', '2819', '2820', '2821', '2822', '2843', '2844', '2845', '2846', '93020', '93021', '93026', '93068', '4405', '4406', '4407', '4408', '4409', '4410', '4440', '4450', '4451', '4452', '95950', '2841', '2842', '2847', '93025', '3069', '6401', '6402', '6403', '6450', '4411', '4412', '4441', '4443', '4444', '2849', '2850', '6405', '6406', '6407', '6408', '6420', '6421', '6422', '6423', '6424', '6460', '9620', '9621', '9622', '9623', '9624', '9601', '9602', '9603', '9604', '9676', '9679', '9670', '9670E', '9670S', '022Y', '4800', '4850', '8001', '9605', '9701', '9701S', '9625', '9626', '9720', '9721', '9722', '9774', '9776', '9777', '9777R', '9770', '9770S', '059D', '4801', '4851', '5501', 'A101', 'A102', 'A120', 'A121', '4620', '4640', '4820', '4840', '4870', 'CGE', 'CGEA', 'CGFA', 'CRC', 'CRCB', 'CRCC', 'CWAA', 'CWB', 'CWBA', 'CWC', 'CWCA', 'CWD', 'CWDA', 'CWF', 'CWFA', 'CWJA', 'CXNA', 'CXPA', 'CXR', 'CXRA', 'A170', '059E', '06EC', '4802', '4852', '5502', 'CEYA', 'CFT', 'CFTB', 'CJT', 'CJTB', 'CNRB', 'CRCA', 'CUDB', 'CUDC', 'CUR', 'CURA', 'CVVA', 'CVVB', 'CVVC', 'CXZ', 'CXZA', 'CYX', 'CYXA', 'A103', 'A104', 'A122', 'A123', 'A175', 'A176', 'DGG', 'DGGA', 'DGGB', 'DNS', 'DNSA', 'A171', 'A172', 'DAB', 'DABA', 'DBC', 'DBCA', 'DBCB', 'DHNA', '1800', 'CDUD', 'CNC', 'CNCC', 'CPNB', 'CTBA', 'CTBB', 'CTBC', 'CTL', 'CTLA', 'CTM', 'CTMA', 'CYN', 'CYNA', 'CYNB', 'CYP', 'CYPA', 'DCN', 'DCNA', 'DHK', 'DHKA', 'DLH', 'DLHB', 'DBXA', 'DBXB', 'AB03', 'AB04', 'DCH', 'DCHA', 'DCJ', 'DCJA', 'DCK', 'DCKA', 'DPE', 'DPEA', 'DDN', 'DDNC', 'DDP', 'DDPA', 'DDPB', 'DJU', 'DJUA', 'DJUB', 'DKDC', 'DKDD', 'DNY', 'DNYA', 'DNZ', 'DNZA', 'DNZB', 'DNZC', 'DPG', 'DPGA', 'DPGB', 'DPH', 'DPHA', 'DPHB', 'CSZ', 'CSZA', 'CVD', 'CVDA', 'CVDD', 'CXT', 'CXTA', 'DBUC', 'DEPA', 'DEPB', 'DERA', 'DERC', 'DGP', 'DGPA', 'DLZ', 'CZA', 'CZAA', 'DCA', 'DCAB', 'DCB', 'DCBE', 'DCU', 'DCUA', 'DFK', 'DFKA', 'DGR', 'DGRC', 'DKN', 'DKNA', 'DLZB', 'DKC', 'DKCA', 'DKK', 'DKKA', 'DRT', 'DRTA', 'DRV', 'DRVA', 'EBE', 'EBEA', 'EBF', 'EBFA', 'EBG', 'EBGA', 'EBGB', '021', '022', '039', '90136', '90137', '90138', '90139', '369', '506', '5061', '5062', '508', '527', '528', '5282', '546', '5462', '5471', '589', '5892', '6161', '6162', '6920', '6921', '6922', '5871', '5872', '61612', '61615', '61616', '6167', '6923', '6923A', '61636', '61639', '61640', '90101', '90102', '90103', '90105', '90106', '90107', '90108', '90109', '90110', '90111', '90113', '90114', '90116', '90117', '90119', '91101', '91102', '91103', '91104', '91106', '91107', '91108', '91151', '91152', '91153', '91157', '91161', '91162', '91163', '91167', '91183', '91191', '91196', 'DKE', 'DKH', '91141', '91142', '91143', '91144', '91146', '91147', '91148', '91149', '91181', '91182', '91184', '91185', '91186', '91189', '91190', '91192', '91193', '91194', '91197', '91198', '91199', '92302', '93002', '93012', '93050', '93051', '93052', '93053', '93054']
glv = ['1KW', '309KW', '92KW', '85KW', '125KW', '110KW', '130KW', '115KW', '154KW', '132KW', '138KW', '150KW', '221KW', '195KW', '177KW', '169KW', '170KW', '74KW', '120KW', '178KW', '228KW', '200KW', '215KW', '212KW', '152KW', '209KW', '118KW', '140KW', '162KW', '184KW', '330KW', '235KW', '243KW', '160KW', '210KW', '191KW', '265KW', '121KW', '155KW', '176KW', '257KW', '220KW', '300KW', '185KW', '168KW', '232KW', '280KW', '336KW', '340KW', '360KW', '250KW', '331KW', '450KW', '239KW', '261KW', '206KW', '180KW', '217KW', '223KW', '368KW', '305KW', '320KW', '353KW', '390KW', '283KW', '298KW', '397KW', '404KW', '213KW', '253KW', '254KW', '188KW', '294KW', '316KW', '386KW', '245KW', '382KW', '419KW', '324KW', '456KW', '281KW', '190KW', '193KW', '350KW', '373KW', '383KW', '426KW', '446KW', '515KW', '447KW', '174KW', '276KW', '272KW', '269KW', '268KW', '338KW', '310KW', '260KW', '240KW', '63KW', '59KW', '53KW', '56KW', '70KW', '65KW', '81KW', '29KW', '32KW', '44KW', '40KW', '51KW', '73KW', '80KW', '55KW', '77KW', '96KW', '66KW', '99KW', '95KW', '117KW', '103KW', '114KW', '493KW', '129KW', '147KW']
items = []
client = MongoClient('192.168.10.170', 27017)
db = client.standard_label
collection = db.standard_label_unique
audiParts = CarsPartNumNew.select()
for audiPart in audiParts:
    if audiPart.pid != '' and ('dropped' not in audiPart.extra_info):
        pid = audiPart.pid
        cid = audiPart.cid
        mid = audiPart.mid
        stand_info = collection.find_one({'pid': pid, 'brandCode': 'porsches'})
        stand_label = stand_info['standard_label'] if stand_info else ''
        if stand_label:
            # 配置代码
            sa_codes = []
            # 发动机排量
            cylinder = []
            # 发动机代码
            vb = []
            # 发动机排量信息
            vb_desc = []
            transmission = []
            drive = []
            diff_mech = []
            power = []
            energy_type = []

            # pSacodes = getSacode(audiPart.sa_code)
            # part_sa_codes = audiPart.sa_code or '{}'
            # part_sa_codes = ujson.loads(part_sa_codes)
            # for key, value in part_sa_codes.items():
            #     if value:
            #         sa_codes = sa_codes + value
            partSacode = audiPart.sa_code or ''
            sa_codes += partSacode.split(',')
            pModel = audiPart.model.replace('<br/>', '')
            pRemark = audiPart.remark

            pModelout = remove_tt(pModel)
            pRemarkout = remove_tt(pRemark)

            if pRemark:
                for key in s:
                    if key in pRemarkout:
                        cy = cleanCylinder(key)
                        cylinder.append(cy)
                for key in pl:
                    if key in pRemark:
                        vb_desc.append(key)

            if pModel:
                for key in bsx:
                    if key in pModelout:
                        tran = cleanTransmission(key)
                        transmission.append(tran)
                for key in qd:
                    if key in pModelout:
                        dr = cleanDrive(key)
                        drive.append(dr)

                for key in s:
                    if key in pModelout:
                        mcy = cleanCylinder(key)
                        cylinder.append(mcy)
                for key in pl:
                    if key in pModel:
                        vb_desc.append(key)
                for key in fdjdm:
                    if key in pModel:
                        vb.append(key)
                if 'PR:' in pModel or 'PR-' in pModel or 'pr:' in pModel or 'pr-' in pModel:
                    # 处理part_model中的PR
                    part_pr = re.sub('[\u4e00-\u9fa5]', '', pModel)
                    part_pr = part_pr.replace('PR:', '')
                    part_pr = part_pr.replace('PR-', '')
                    part_pr = part_pr.replace('+', ',')
                    part_pr = part_pr.replace('/', ',')
                    part_pr = part_pr.replace(':', ',')
                    part_pr = part_pr.replace('_', ',')
                    part_pr = part_pr.split(',')
                    for j in part_pr:
                        sa_codes.append(j)

            sa_codes = set(sa_codes)
            sa_codes = list(sa_codes)

            vb = set(vb)
            vb = list(vb)

            for i in sa_codes:
                # contrast = CarsSaDesc.get_or_none(CarsSaDesc.sa == i, CarsSaDesc.lang == 'zh', CarsSaDesc.brand_code == 'porsches')
                contrast = CarsSaDesc.get_or_none(CarsSaDesc.sa == i, CarsSaDesc.lang == 'zh', CarsSaDesc.brand_code == audiPart.brand_code)
                if contrast and contrast.desc:
                    saDescout = remove_tt(contrast.desc)
                    # model_desc.append(contrast.desc)
                    if '发动机' in contrast.desc:
                        # 筛选出sa表desc中的排量信息
                        for key in pl:
                            if key in contrast.desc:
                                vb_desc.append(key)
                        for key in s:
                            if key in saDescout:
                                key = cleanCylinder(key)
                                cylinder.append(key)
                        for key in fuel:
                            if key in saDescout:
                                energy_type.append(key)
                        for key in glv:
                            if key in saDescout:
                                key = cleanPower(key)
                                power.append(key)

                    # 处理变速器配置
                    if ('变速箱' in contrast.desc or '变速器' in contrast.desc) and ('档' in contrast.desc or '无级' in contrast.desc):
                        for key in bsx:
                            if key in saDescout:
                                key = cleanTransmission(key)
                                transmission.append(key)

                    # 处理驱动配置
                    for key in qd:
                        if key in saDescout:
                            key = cleanDrive(key)
                            drive.append(key)

                    # 处理差速器配置
                    if '差速' in contrast.desc or '减速' in contrast.desc:
                        diff_mech.append(contrast.desc)

            for i in vb:
                if cid.isdigit():
                    vb_AF = Vb.get_or_none(Vb.aa == str(int(cid)), Vb.ab.contains(i), Vb.brand == audiPart.brand_code)
                elif cid.startswith('U'):
                    vb_AF = Vb.get_or_none(Vb.aa == cid[1:], Vb.ab.contains(i), Vb.brand == audiPart.brand_code)
                else:
                    vb_AF = Vb.get_or_none(Vb.aa == cid, Vb.ab.contains(i), Vb.brand == audiPart.brand_code)
                if vb_AF:
                    if vb_AF.af:
                        vb_desc.append(str(int(vb_AF.af) / 100))
                    if vb_AF.ai:
                        cylinder.append(vb_AF.ai)
                    if vb_AF.ag:
                        power.append(vb_AF.ag)

            # 处理part时间
            date, colorDate, year = audi_do_filter_extra_info(audiPart.extra_info)
            dateLeft, dateRight = mergeDate(date)
            colordateLeft, colordateRight = mergeDate(colorDate)
            yearLeft, yearRight = mergeDate(year)

            leftList = []
            if dateLeft:
                leftList.append(dateLeft)
            if colordateLeft:
                leftList.append(colordateLeft)
            if yearLeft:
                leftList.append(yearLeft)
            rightList = []
            if dateRight:
                rightList.append(dateRight)
            if colordateRight:
                rightList.append(colordateRight)
            if yearRight:
                rightList.append(yearRight)
            mdateLeft = max(leftList) if leftList else ''
            mdateRight = min(rightList) if rightList else ''
            merge_date = mdateLeft + ('-' if mdateLeft or mdateRight else '') + mdateRight

            vb_desc = set(vb_desc)
            vb_desc = list(vb_desc)
            vb_desc = ','.join(vb_desc)

            cylinder = set(cylinder)
            cylinder = list(cylinder)
            cylinder = ','.join(cylinder)

            transmission = set(transmission)
            transmission = list(transmission)
            transmission = ','.join(transmission)

            drive = set(drive)
            drive = list(drive)
            drive = ','.join(drive)

            diff_mech = set(diff_mech)
            diff_mech = list(diff_mech)
            diff_mech = ','.join(diff_mech)

            energy_type = set(energy_type)
            energy_type = list(energy_type)
            energy_type = ','.join(energy_type)
            power = set(power)
            power = list(power)
            power = ','.join(power)
            vb = ','.join(vb)

            ssa_codes = []
            # 发动机排量
            scylinder = []
            # 发动机代码
            svb = []
            # 发动机排量信息
            svb_desc = []
            stransmission = []
            sdrive = []
            sdiff_mech = []
            senergy_type = []
            spower = []
            merge_sdate = ''
            audiStructure = CarsStructureCollectionNew.get_or_none(CarsStructureCollectionNew.cid == audiPart.cid, CarsStructureCollectionNew.mid == audiPart.mid)

            # sSacodes = getSacode(audiStructure.sa_code)
            if audiStructure:
                # structure_sa_codes = audiStructure.sa_code or '{}'
                # structure_sa_codes = ujson.loads(structure_sa_codes)
                # for key, value in structure_sa_codes.items():
                #     if value:
                #         ssa_codes = ssa_codes + value
                structureSacode = audiStructure.sa_code or ''
                ssa_codes += structureSacode.split(',')
                sModel = audiStructure.model.replace('<br/>', '')
                sDescription = audiStructure.description

                sModelout = remove_tt(sModel)
                sDescriptionout = remove_tt(sDescription)

                if sDescription:
                    for key in s:
                        if key in sDescriptionout:
                            key = cleanCylinder(key)
                            scylinder.append(key)
                    for key in pl:
                        if key in sDescription:
                            svb_desc.append(key)

                if sModel:
                    for key in bsx:
                        if key in sModelout:
                            key = cleanTransmission(key)
                            stransmission.append(key)
                    for key in qd:
                        if key in sModelout:
                            key = cleanDrive(key)
                            sdrive.append(key)

                    for key in s:
                        if key in sModelout:
                            key = cleanCylinder(key)
                            scylinder.append(key)
                    for key in pl:
                        if key in sModel:
                            svb_desc.append(key)
                    for key in fdjdm:
                        if key in sModel:
                            svb.append(key)
                    if 'PR:' in sModel or 'PR-' in sModel or 'pr:' in sModel or 'pr-' in sModel:
                        # 处理part_model中的PR
                        s_pr = re.sub('[\u4e00-\u9fa5]', '', sModel)
                        s_pr = s_pr.replace('PR:', '')
                        s_pr = s_pr.replace('PR-', '')
                        s_pr = s_pr.replace('+', ',')
                        s_pr = s_pr.replace('/', ',')
                        s_pr = s_pr.replace(':', ',')
                        s_pr = s_pr.replace('_', ',')
                        s_pr = s_pr.split(',')
                        for j in s_pr:
                            ssa_codes.append(j)

                for i in ssa_codes:
                    contrast = CarsSaDesc.get_or_none(CarsSaDesc.sa == i, CarsSaDesc.lang == 'zh', CarsSaDesc.brand_code == audiPart.brand_code)
                    if contrast and contrast.desc:
                        ssaDescout = remove_tt(contrast.desc)
                        # model_desc.append(contrast.desc)
                        if '发动机' in contrast.desc:
                            # 筛选出sa表desc中的排量信息
                            for key in pl:
                                if key in contrast.desc:
                                    svb_desc.append(key)
                            for key in s:
                                if key in ssaDescout:
                                    key = cleanCylinder(key)
                                    scylinder.append(key)
                            for key in fuel:
                                if key in ssaDescout:
                                    senergy_type.append(key)
                            for key in glv:
                                if key in ssaDescout:
                                    key = cleanPower(key)
                                    spower.append(key)

                        # 处理变速器配置
                        if ('变速箱' in contrast.desc or '变速器' in contrast.desc) and ('档' in contrast.desc or '无级' in contrast.desc):
                            for key in bsx:
                                if key in ssaDescout:
                                    key = cleanTransmission(key)
                                    stransmission.append(key)

                        # 处理驱动配置
                        for key in qd:
                            if key in ssaDescout:
                                key = cleanDrive(key)
                                sdrive.append(key)

                        # 处理差速器配置
                        if '差速' in contrast.desc or '减速' in contrast.desc:
                            sdiff_mech.append(contrast.desc)

                for i in svb:
                    if cid.isdigit():
                        vb_AF = Vb.get_or_none(Vb.aa == str(int(cid)), Vb.ab.contains(i), Vb.brand == audiPart.brand_code)
                    elif cid.startswith('U'):
                        vb_AF = Vb.get_or_none(Vb.aa == cid[1:], Vb.ab.contains(i), Vb.brand == audiPart.brand_code)
                    else:
                        vb_AF = Vb.get_or_none(Vb.aa == cid, Vb.ab.contains(i), Vb.brand == audiPart.brand_code)
                    if vb_AF:
                        if vb_AF.af:
                            svb_desc.append(str(int(vb_AF.af) / 100))
                        if vb_AF.ai:
                            scylinder.append(vb_AF.ai)
                        if vb_AF.ag:
                            spower.append(vb_AF.ag)

                sdate, scolordate, syear = audi_do_filter_extra_info(audiStructure.extra_info)
                sdateLeft, sdateRight = mergeDate(sdate)
                scolordateLeft, scolordateRight = mergeDate(scolordate)
                syearLeft, syearRight = mergeDate(syear)
                # sleftList = [sdateLeft, scolordateLeft, syearLeft]
                # srightList = [sdateRight, scolordateRight, syearRight]
                sleftList = []
                if sdateLeft:
                    sleftList.append(sdateLeft)
                if scolordateLeft:
                    sleftList.append(scolordateLeft)
                if syearLeft:
                    sleftList.append(syearLeft)
                srightList = []
                if sdateRight:
                    srightList.append(sdateRight)
                if scolordateRight:
                    srightList.append(scolordateRight)
                if syearRight:
                    srightList.append(syearRight)
                msdateLeft = max(sleftList) if sleftList else ''
                msdateRight = min(srightList) if srightList else ''
                merge_sdate = msdateLeft + ('-' if msdateLeft or msdateRight else '') + msdateRight

            svb_desc = set(svb_desc)
            svb_desc = list(svb_desc)
            svb_desc = ','.join(svb_desc)

            scylinder = set(scylinder)
            scylinder = list(scylinder)
            scylinder = ','.join(scylinder)

            stransmission = set(stransmission)
            stransmission = list(stransmission)
            stransmission = ','.join(stransmission)

            sdrive = set(sdrive)
            sdrive = list(sdrive)
            sdrive = ','.join(sdrive)

            sdiff_mech = set(sdiff_mech)
            sdiff_mech = list(sdiff_mech)
            sdiff_mech = ','.join(sdiff_mech)

            senergy_type = set(senergy_type)
            senergy_type = list(senergy_type)
            senergy_type = ','.join(senergy_type)
            spower = set(spower)
            spower = list(spower)
            spower = ','.join(spower)
            svb = ','.join(svb)

            unit = {'pid': audiPart.pid, 'cid': audiPart.cid, 'mid': audiPart.mid, 'brand_code': audiPart.brand_code,
                    'standard_label': stand_label, 'engine_code': vb,
                    'cylinder': cylinder, 'transmission': transmission, 'drive': drive, 'diff_mech': diff_mech,
                    'displacement': vb_desc, 'pid_date': merge_date, 'energy_type': energy_type, 'power': power,
                    'group_engine_code': svb, 'group_cylinder': scylinder, 'group_transmission': stransmission,
                    'group_drive': sdrive, 'group_diff_mech': sdiff_mech, 'group_displacement': svb_desc,
                    'group_date': merge_sdate, 'group_energy_type': senergy_type, 'group_power': spower}
            print(unit)
            items.append(unit)

df = pd.DataFrame(items)
df['pid'] = df['pid'].astype('str')
df.to_csv('./export/porsches_parts.csv', mode='a', index=False)